﻿using System.Data;
using System.Web.UI.WebControls;

namespace BaseUI
{
    public class PublicMethod
    {
        public PublicMethod()
        {
        }

        /// <summary>
        /// 为 DropDownList 绑定层级
        /// </summary>
        /// <param name="ddl">DropDownList 控件</param>
        /// <param name="dt">数据源</param>
        /// <param name="ValueName">控件值的列名称</param>
        /// <param name="DisplayName">控件显示的列名称</param
        /// <param name="isChoice">是否添加请选择项（如果添加请选择项，请选择项的值为-1）</param>
        public static void BindDDL(System.Web.UI.WebControls.DropDownList ddl, DataTable dt, string ValueName, string DisplayName, bool isChoice)
        {
            ddl.Items.Clear();
            if (isChoice)
            {
                DataRow dr = dt.NewRow();
                dr[ValueName] = "-1";
                dr[DisplayName] = "--请选择--";
                dt.Rows.InsertAt(dr, 0);
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem MyListItem = new ListItem();
                MyListItem.Text = dt.Rows[i][DisplayName].ToString();
                MyListItem.Value = dt.Rows[i][ValueName].ToString();
                ddl.Items.Add(MyListItem);
            }
        }

        /// <summary>
        /// 为 DropDownList 绑定层级
        /// </summary>
        /// <param name="ddl">DropDownList 控件</param>
        /// <param name="dt">数据源</param>
        /// <param name="ValueName">控件值的列名称</param>
        /// <param name="DisplayName">控件显示的列名称</param>
        /// <param name="SuperiorName">上级列名称</param>
        /// <param name="StartValue">第一级的值</param>
        /// <param name="isChoice">是否添加请选择项（如果添加请选择项，请选择项的值为-1）</param>
        public static void BindDropDownList(System.Web.UI.WebControls.DropDownList ddl, DataTable dt, string ValueName, string DisplayName, string SuperiorName, string StartValue, bool isChoice)
        {
            ddl.Items.Clear();
            int idx = -1;

            for (int n = 0; n < dt.Rows.Count; n++)
            {
                if (dt.Rows[n][SuperiorName].ToString() == StartValue)
                {
                    idx = idx + 1;
                    addItem(ddl, dt, ValueName, DisplayName, SuperiorName, dt.Rows[n][ValueName].ToString(), dt.Rows[n][DisplayName].ToString(), idx);
                    idx = idx - 1;
                }
            }

            if (isChoice)
            {
                //DataRow dr = dt.NewRow();
                //dr[ValueName] = "-1";
                //dr[SuperiorName] = "0";
                //dr[DisplayName] = "--请选择--";
                //dt.Rows.InsertAt(dr, 0);
                //StartValue = "0";

                ddl.Items.Insert(0, new ListItem("--请选择--", "-1"));
            }
        }

        private static void addItem(System.Web.UI.WebControls.DropDownList lb, DataTable dt, string ValueName, string DisplayName, string SuperiorName, string sId, string sName, int idx)
        {
            if (dt.Rows.Count > 0)
            {
                string sn = sName;
                for (int n = 0; n < idx; n++)
                {
                    sn = (char)58853 + "" + sn;
                }
                System.Web.UI.WebControls.ListItem it = new ListItem(sn, sId);

                lb.Items.Add(it);

                for (int n = 0; n < dt.Rows.Count; n++)
                {
                    if (dt.Rows[n][SuperiorName].ToString() == sId)
                    {
                        idx = idx + 1;
                        addItem(lb, dt, ValueName, DisplayName, SuperiorName, dt.Rows[n][ValueName].ToString(), dt.Rows[n][DisplayName].ToString(), idx);
                        idx = idx - 1;
                    }
                }
            }
        }
    }
}